package gouhuo.allbugs.top.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import gouhuo.allbugs.top.common.BaseResponse;
import gouhuo.allbugs.top.common.BusinessException;
import gouhuo.allbugs.top.common.ErrorCode;
import gouhuo.allbugs.top.model.domain.Team;
import gouhuo.allbugs.top.model.domain.UserTeam;
import gouhuo.allbugs.top.model.request.team.*;
import gouhuo.allbugs.top.model.vo.TeamUserVO;
import gouhuo.allbugs.top.model.vo.UserVO;
import gouhuo.allbugs.top.service.TeamService;
import gouhuo.allbugs.top.service.UserService;
import gouhuo.allbugs.top.service.UserTeamService;
import gouhuo.allbugs.top.utils.ResultUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 队伍
 */
@RestController
@RequestMapping("/team")
public class TeamController {

    @Resource
    private UserService userService;

    @Resource
    private TeamService teamService;

    @Resource
    private UserTeamService userTeamService;

    @ApiOperation("创建队伍")
    @PostMapping("/add")
    public BaseResponse<Integer> addTeam(@RequestBody TeamAddRequest teamAddRequest, HttpServletRequest request) {
        if (teamAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        UserVO userVO = userService.getLoginUser(request, null);
        Team team = new Team();
        BeanUtils.copyProperties(teamAddRequest, team);
        teamService.addTeam(team, userVO);
        return ResultUtil.success(200);
    }

    @ApiOperation("修改队伍信息")
    @PostMapping("/update")
    public BaseResponse<Integer> updateTeam(@RequestBody TeamUpdateRequest teamUpdateRequest, HttpServletRequest request) {
        if (teamUpdateRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        UserVO userVO = userService.getLoginUser(request, null);
        boolean result = teamService.updateTeam(teamUpdateRequest, userVO);
        if (!result) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "更新失败");
        }
        return ResultUtil.success(200);
    }

    @ApiOperation("获取队伍的详细信息")
    @GetMapping("/get")
    public BaseResponse<TeamUserVO> getTeamById(@RequestParam("id") long id) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Team team = teamService.getById(id);
        if (team == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        return ResultUtil.success(BeanUtil.copyProperties(team, TeamUserVO.class));
    }

    @ApiOperation("加入队伍")
    @PostMapping("/join")
    public BaseResponse<Boolean> joinTeam(@RequestBody TeamJoinRequest teamJoinRequest, HttpServletRequest request) {
        if (teamJoinRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        UserVO userVO = userService.getLoginUser(request, null);
        boolean result = teamService.joinTeam(teamJoinRequest, userVO);
        return ResultUtil.success(result);
    }

    @ApiOperation("退出队伍")
    @PostMapping("/quit")
    public BaseResponse<Boolean> quitTeam(@RequestBody TeamQuitRequest teamQuitRequest, HttpServletRequest request) {
        if (teamQuitRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        UserVO userVO = userService.getLoginUser(request, null);
        boolean result = teamService.quitTeam(teamQuitRequest, userVO);
        return ResultUtil.success(result);
    }

    @ApiOperation("解散队伍")
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteTeam(@RequestBody TeamQuitRequest deleteRequest, HttpServletRequest request) {
        if (deleteRequest == null || deleteRequest.getTeamId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        UserVO userVO = userService.getLoginUser(request, null);
        long id = deleteRequest.getTeamId();
        boolean result = teamService.deleteTeam(id, userVO);
        if (!result) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "解散失败");
        }
        return ResultUtil.success(true);
    }

    @ApiOperation("获取队伍列表")
    @GetMapping("/list")
    public BaseResponse<List<TeamUserVO>> listTeams(TeamListRequest teamListRequest, HttpServletRequest request) {
        if (teamListRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        UserVO userVO = userService.getLoginUser(request, null);
        if (userVO == null) {
            throw new BusinessException(ErrorCode.NOT_LOGIN);
        }
        boolean isAdmin = userService.isAdmin(request);
        //不展示自己创建的和已加入的队伍
        LambdaQueryWrapper<UserTeam> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(UserTeam::getUserId, userVO.getId()).ne(UserTeam::getTies, 0);
        List<UserTeam> notShowList = userTeamService.list(lambdaQueryWrapper);
        List<Long> notShowIdList = notShowList.stream().map(UserTeam::getTeamId).collect(Collectors.toList());
        // 1、查询队伍列表
        List<TeamUserVO> teamList = teamService.listTeams(teamListRequest, isAdmin, notShowIdList);
        if (teamList.size() == 0) {
            return ResultUtil.success(teamList);
        }
        List<Long> teamIdList = teamList.stream().map(TeamUserVO::getId).collect(Collectors.toList());
        List<UserTeam> userTeamList = userTeamService.list(new QueryWrapper<UserTeam>().in("teamId", teamIdList).ne("ties", 0));
        // 3、查询已加入队伍的人数
        teamService.joinUserNum(userTeamList, teamList, false);
        return ResultUtil.success(teamList);
    }

}
